'use strict';

angular.module('app').controller('positionCtrl', ['$log','$q','$http','$state','$scope','cache', function($log,$q,$http,$state,$scope,cache){
	$scope.isLogin=!!cache.get('name');
	$scope.message=$scope.isLogin?'投个简历':'去登陆'
	function getPosition(){
		var def=$q.defer();//延迟加载
		$http.get('data/position.json',{
			parans:{
				id:$state.params.id
			}
		}).success(function(resp){
			$scope.position=resp;
			if(resp.posted){
				$scope.message='已投递'
			}
			def.resolve(resp);
		}).error(function(err) {
			def.reject(err);
		});
		return def.promise;
	}
	function getCompany(id){
		$http.get('data/company.json?id'+id).then(function(resp){
			$scope.company=resp.data;
		})
	}
	getPosition().then(function(obj){
		getCompany(obj.companyId);
	});
	$scope.go=function(){
		if($scope.message!=='已投递'){
			if($scope.isLogin){
				$http.post('data/handle.json',{
					id:$scope.position.id,
				}).success(function(resp){
					$log.info(resp);
					$scope.message='已投递'
				});
			}else{
				$scope.go('login')
			}
		}
	}
}])